package com.example.xui.Main;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xui.Comment_Article;
import com.example.xui.Daka.Daka;
import com.example.xui.R;
import com.example.xui.SearchActivity;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;
import com.tapadoo.alerter.Alerter;
import com.wyt.searchbox.SearchFragment;
import com.wyt.searchbox.custom.IOnSearchClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {
    private CarouselView carouselView;
    private EditText editText;
    private ImageView imageView;
    static int i=0;
    private ImageView daka;
    private TextView mTitleid;
    private TextView mTitleContent;
    private ImageView pictureto;
    private TextView mTitledTitle;
    private TextView favorite;
    private TextView comment;
    private TextView classification;
    int[] sampleImages = {R.drawable.school1,R.drawable.school2, R.drawable.school3};
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<JSONObject> list1=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        carouselView = (CarouselView) getActivity().findViewById(R.id.carouselView);

        carouselView.setImageListener(imageListener);
        carouselView.setPageCount(sampleImages.length);
        carouselView.setImageClickListener(new ImageClickListener() {
            @Override
            public void onClick(int position) {
               /* Intent intent=new Intent(getActivity().getApplicationContext(),SearchFragment.class);
                intent.putExtra("suo",position);
                startActivity(intent);*/
                Toast.makeText(getContext(), "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
            }
        });
        final SearchFragment searchFragment = SearchFragment.newInstance();
        searchFragment.setOnSearchClickListener(new IOnSearchClickListener() {
            @Override
            public void OnSearchClick(String keyword) {
                //这里处理逻辑
              //  Toast.makeText(getContext(), keyword, Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(getContext(), SearchActivity.class);
                intent.putExtra("suo",keyword);
                startActivity(intent);
            }
        });
        imageView=getActivity().findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchFragment.showFragment(getActivity().getSupportFragmentManager(),SearchFragment.TAG);

            }
        });
        if(i==0) {
            Alerter.create(getActivity())
                    .setTitle("欢迎来到知行苑")
                    .setText("天才和傻子是相对而言的，只有努力的人才能成为天才，也只有懒惰的人才能成为傻子。")
                    .setIcon(R.drawable.hello)
                    .setBackgroundColor(R.color.colorAccent)
                    .show();
            i++;
        }
        daka=getActivity().findViewById(R.id.imageView2);
        daka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), Daka.class);
                startActivity(intent);
            }
        });
        list1=Load_Data();
       /* try {
            Log.i("Tag","tt="+list1.get(0).getString("resourceID"));
        } catch (JSONException e) {
            e.printStackTrace();
        }*/
       /* mRecyclerView = getActivity().findViewById(R.id.popular);
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        String son=getActivity().getIntent().getStringExtra("sno");*/
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static FirstFragment newInstance() {
        FirstFragment fragment1 = new FirstFragment();
        return fragment1;
    }
    ImageListener imageListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, ImageView imageView) {
            imageView.setImageResource(sampleImages[position]);
        }
    };

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.adapter_note1, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            try {
                holder.pictureto.setImageBitmap(BitmapFactory.decodeFile(list1.get(position).getString("resourcePicture")));
                //holder.mTitleContent.setText(list1.get(position).getString("resourceContent"));
                holder.mTitleid.setText(list1.get(position).getString("resourceNickname"));
                holder.mTitledTitle.setText(list1.get(position).getString("resourceTitle"));
                holder.classification.setText(list1.get(position).getString("resourceType"));
                holder.likenum.setText(list1.get(position).getString("resourceThumbNum"));
                holder.commentnum.setText(list1.get(position).getString("resourceConmentNum"));
                holder.hear_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getContext(), Comment_Article.class);
                        try {
                            intent.putExtra("title",list1.get(position).getString("resourceTitle"));
                            intent.putExtra("id",list1.get(position).getString("resourceNickname"));
                            intent.putExtra("picture",list1.get(position).getString("resourcePicture"));
                            intent.putExtra("connect",list1.get(position).getString("resourceContent"));
                            intent.putExtra("resourceThumbNum",list1.get(position).getString("resourceThumbNum"));
                            intent.putExtra("resourceConmentNum",list1.get(position).getString("resourceConmentNum"));
                            intent.putExtra("resourceData",list1.get(position).getString("resourceData"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        startActivity(intent);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // resourceTableVo news = list.get(position);
            //holder.comment.setText(news.getResourceContent());
        }

        @Override
        public int getItemCount() {
            return list1.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleid;
       // TextView mTitleContent;
        ImageView pictureto;
        TextView mTitledTitle;
        TextView classification;
        TextView likenum;
        TextView commentnum;
        ConstraintLayout hear_view;
        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            pictureto=itemView.findViewById(R.id.imageView17);
            mTitleid = itemView.findViewById(R.id.deviceName);
           // mTitleContent = itemView.findViewById(R.id.deviceAddress);
            mTitledTitle=itemView.findViewById(R.id.textView9);
            classification=itemView.findViewById(R.id.textView47);
            likenum=itemView.findViewById(R.id.textView51);
            commentnum=itemView.findViewById(R.id.textView53);
            hear_view=itemView.findViewById(R.id.heat_view);
        }
    }
    public List<JSONObject> Load_Data() {
        final List<JSONObject>[] list = new List[]{new ArrayList<>()};
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("Tag", "id1=" + 123);
                RequestParams params = new RequestParams("http://10.138.87.49:8080/resource/resoucrceHot");
                params.addHeader("Content-Type", "application/x-www-form-urlencoded");
                x.http().post(params, new Callback.CacheCallback<JSONArray>() {
                    @Override
                    public void onSuccess(JSONArray result) {
                        Log.i("Tag", "id1=" + 1234);
                        //   resourceTableVo re[] = new resourceTableVo[result.length()];
                        List<JSONObject> list=new ArrayList<>();

                        for (int i = 0; i < result.length(); i++) {
                            try {
                                JSONObject jsonObject = result.getJSONObject(i);
                                list.add(jsonObject);
                             //   Thread.sleep(2000);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            getActivity().runOnUiThread(new Runnable() {
                                @Override

                                public void run() {
                                    list1=list;
                                    mRecyclerView = getActivity().findViewById(R.id.popular);
                                    mMyAdapter = new MyAdapter();
                                    mRecyclerView.setAdapter(mMyAdapter);
                                    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                                    mRecyclerView.setLayoutManager(layoutManager);
                                    String son=getActivity().getIntent().getStringExtra("sno");
                                    try {
                                        Log.i("Tag","tt="+list1.get(0).getString("resourceID"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
// refresh ui 的操作代码             list1.a
                                }

                            });

                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("sds", "网络错误" + params.toString());
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(JSONArray result) {
                        return false;
                    }
                });

            }
        }).start();
        return list1;
    }



}
